package com.youseu.taoliving.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.youseu.taoliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.youseu.taoliving.commodity.entity.AttrEntity;
import com.youseu.taoliving.commodity.service.AttrAttrgroupRelationService;
import com.youseu.taoliving.commodity.service.AttrService;
import com.youseu.taoliving.commodity.service.CategoryService;
import com.youseu.taoliving.commodity.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.youseu.taoliving.commodity.entity.AttrgroupEntity;
import com.youseu.taoliving.commodity.service.AttrgroupService;
import com.youseu.common.utils.PageUtils;
import com.youseu.common.utils.R;

import javax.annotation.Resource;


/**
 * 家居商品属性分组
 *
 * @author tao
 * @email tao@gmail.com
 * @date 2024-08-03 09:40:55
 */
@RestController
@RequestMapping("commodity/attrgroup")
public class AttrgroupController {
    @Autowired
    private AttrgroupService attrgroupService;
    @Resource
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrgroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据查询条件和categoryId查询列表
     */
    @RequestMapping("/list/{categoryId}")
    // @RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Long categoryId){
        PageUtils page = attrgroupService.queryPage(params,categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("commodity:attrgroup:info")
    public R info(@PathVariable("id") Long id){
		AttrgroupEntity attrgroup = attrgroupService.getById(id);
		//设置层级id数组 用于前端显示
        Long categoryId = attrgroup.getCategoryId();
        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(categoryId);
        attrgroup.setCascadedCategoryId(cascadedCategoryId);
        return R.ok().put("attrgroup", attrgroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("commodity:attrgroup:save")
    public R save(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.save(attrgroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("commodity:attrgroup:update")
    public R update(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.updateById(attrgroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("commodity:attrgroup:delete")
    public R delete(@RequestBody Long[] ids){
		attrgroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @Resource
    private AttrService attrService;

    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable Long attrGroupId){
        List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data",attrEntityList);
    }
    @RequestMapping("/attr/relation/delete")
    public R attrDelRelation(@RequestBody AttrAttrgroupRelationEntity[] relationEntities){
        attrService.deleteBatchRelation(relationEntities);
        return R.ok();
    }

    @RequestMapping("/{attrGroupId}/attr/allowrelation")
    public R attrAllowRelation(@PathVariable Long attrGroupId,
                               @RequestParam Map<String ,Object> params){
        PageUtils pageUtils = attrService.getAllowRelationAttr(params, attrGroupId);
        return R.ok().put("page",pageUtils);
    }

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    //添加关联(支持批量关联)
    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities){
        attrAttrgroupRelationService.saveBatch(relationEntities);
        return R.ok();
    }

    @RequestMapping("/{catalogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable Long catalogId){
        List<AttrGroupWithAttrsVo> list = attrgroupService.getAttrGroupWithAttrsByCategoryId(catalogId);
        return R.ok().put("data",list);
    }
}
